package  dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class DaoImpl<T> implements IDao<T> {
	@Autowired
	private SessionFactory sessionFactory;
	@Autowired
	HibernateTemplate jdbcTemplate;
	@Override
	public T save(T obj) {
		/*if(sessionFactory==null)	return null;
		Session session=sessionFactory.openSession();
		Transaction tx=session.beginTransaction();
		try{
			session.persist(obj);
			tx.commit();
		}catch(Exception e){
			tx.rollback();
			e.printStackTrace();
		}finally{
			session.close();
			return obj;
		}*/
		jdbcTemplate.save(obj);
		return obj;
	}

	@Override
	public boolean update(T obj) {
//		boolean judge=false;
//		if(sessionFactory==null)	return false;
//		Session session=sessionFactory.openSession();
//		Transaction tx=session.beginTransaction();
//		try{
//			session.update(obj);
//			judge=true;
//			tx.commit();
//		}catch(Exception e){
//			tx.rollback();
//			e.printStackTrace();
//		}finally{
//			session.close();
//			return judge;
//		}
		jdbcTemplate.update(obj);
		return true;
	}

	@Override
	public boolean delete(Class<? extends T> clazz,String rowid) {
//		boolean judge=false;
//		if(sessionFactory==null)	return false;
//		Session session=sessionFactory.openSession();
////		Transaction tx=session.beginTransaction();
//		try{
//			Object obj=session.get(clazz, rowid);
//			if(obj==null)  judge= false;
//			session.delete(obj);
//			judge=true;
////			tx.commit();
//		}catch(Exception e){
////			tx.rollback();
//			e.printStackTrace();
//		}finally{
//			session.close();
//			return judge;
//		}
		T entity=this.queryById(clazz, rowid);
		jdbcTemplate.delete(entity);
		return true;
	}

	@Override
	public T queryById(Class<? extends T> clazz,String rowid) {
//		if(sessionFactory==null||rowid==null)	return null;
//		Session session=sessionFactory.openSession();
//		Transaction tx=session.beginTransaction();
//		try{
//			Object obj=session.get(clazz, rowid);
//			tx.commit();
//			return (T)obj;
//		}catch(Exception e){
//			tx.rollback();
//			e.printStackTrace();
//			return null;
//		}finally{
//			session.close();
//		}
		return jdbcTemplate.get(clazz, rowid);
	}

	@Override
	public List<T> queryBySql(Class<? extends T> clazz,String sql) {
		List<T> list;
		if(sessionFactory==null)	return null;
		Session session=sessionFactory.openSession();
		Transaction tx=session.beginTransaction();
		try{
			 list=(List<T>)session.createSQLQuery(sql).addEntity(clazz).list();
			 tx.rollback();
			 return list;
		}catch(Exception e){
			tx.commit();
			e.printStackTrace();
			return null;
		}finally{
			session.close();
		}
	}

	@Override
	public List<T> queryByHql(String hql) {
		// TODO Auto-generated method stub
		return jdbcTemplate.find(hql);
	}
	@Override
	public List<T> queryByHql(String hql,Object value) {
		// TODO Auto-generated method stub
		return jdbcTemplate.find(hql,value);
	}
	@Override
	public HibernateTemplate getHibernateTemplate(){
		return jdbcTemplate;
	}
	
	@Override
	public Session getSession() {
		return  sessionFactory.openSession();
	}

	@Override
	public List<T> queryByHql(String hql, Object[] values) {
		return jdbcTemplate.find(hql, values);
	}
}
